package com.cdshangpai.utils.offlinecache.aboutus;

import android.util.Log;

import com.cdshangpai.photoapp.LogConfig;
import com.cdshangpai.utils.api.Api;
import com.cdshangpai.utils.db.beans.Aboutus;
import com.cdshangpai.utils.offlinecache.DbVersion;
import com.cdshangpai.utils.offlinecache.base.BaseCacheProcessor;
import com.cdshangpai.utils.offlinecache.base.DataDownloader;
import com.cdshangpai.utils.offlinecache.base.query.AboutusQuery;

import java.util.ArrayList;
import java.util.List;

public class AboutusCacheProcessor {
    AboutusQuery aboutusQuery;
    Api api;
    static NotifyUpdate notifyUpdate;

    public static void registNotifyUpdate(NotifyUpdate notifyUpdate) {
        Log.i(LogConfig.logTag,"reg update");
        AboutusCacheProcessor.notifyUpdate = notifyUpdate;
    }

    public static void unregistNotifyUpdate() {
        Log.i(LogConfig.logTag,"unreg update");
        notifyUpdate = null;
    }

    public AboutusCacheProcessor() {
        aboutusQuery = new AboutusQuery();
        api = new Api();
    }

    public boolean begin(DbVersion dbVersion) {
        DataDownloader dataDownloader = new DataDownloader() {
            @Override
            public List download(List ids) {
                Aboutus aboutus = api.queryAboutus();
                if (aboutus == null) {
                    return null;
                }
                ArrayList<Aboutus> result = new ArrayList<>();
                result.add(aboutus);
                return result;
            }
        };
        List<DbVersion> serverVideoVersions = new ArrayList<>();
        serverVideoVersions.add(dbVersion);
        List<Aboutus> localDatas = aboutusQuery.query();

        BaseCacheProcessor baseCacheProcessor = new BaseCacheProcessor<Aboutus>()
                .setDbQuery(aboutusQuery)
                .setDataDownloader(dataDownloader)
                .setLocalDatas(localDatas)
                .setServerVersion(serverVideoVersions)
                .forAboutus();
        boolean success = baseCacheProcessor.begin();
        //通知首页更新数据显示
        if(success&&baseCacheProcessor.getHaveUpdate()&&notifyUpdate!=null)
        {
            notifyUpdate.notifyUpdate();
        }
        return success;
    }
}
